/**
 * Created by lx on 2018/4/3.
 */
CommonUtils.regNamespace("com.im.projectInfoManage.itemStaticManage");

$(function() {
    com.im.projectInfoManage.itemStaticManage.invoke.init();
});

com.im.projectInfoManage.itemStaticManage.invoke = function() {
    var client = new com.im.common.ajax();

    /**
     * 页面初始化
     * @private
     */
    function _init() {
        //初始化时间、复选框、下拉框等事件和样式
        _initCommpentAction();
        //获取规划类型列表
        $("#vPlanTypesSelect").constantCommpent({
    	    elmIdSel : "vPlanTypesSelect",//需要展示的位置下拉框
    	    constParamCode : "PLAN_PROJ_CLASS",//需要加载的常量编码
    	    callbackFunc: function (objvalue, objname) {//下拉框值改变时回调
    	    	_querySubjects(1);
    	    }
    	});
        //查询项目进度列表
        $("#vSubjectsBtn").on("click", function () {
            _querySubjects(1);
        }).trigger("click");
        //条件重置
        $("#vResetCond").on("click", _resetCond);
    }

    function _initCommpentAction() {
        //时间插件
        $('#startDatepicker').datepicker({
            autoclose: true,
        }).on('changeDate',function () {
            $('#addEditProjectForm').data('bootstrapValidator').updateStatus('startDatepicker', 'NOT_VALIDATED',null).validateField('startDatepicker');
            $('#addEditProjectForm').data('bootstrapValidator').updateStatus('endDatepicker', 'NOT_VALIDATED',null).validateField('endDatepicker');
        });
        $('#endDatepicker').datepicker({
            autoclose: true,
        }).on('changeDate',function () {
            $('#addEditProjectForm').data('bootstrapValidator').updateStatus('endDatepicker', 'NOT_VALIDATED',null).validateField('endDatepicker');
        });

        //复选框
        $('input[type="checkbox"].minimal, input[type="radio"].minimal').iCheck({
            checkboxClass: 'icheckbox_minimal-blue',
            radioClass   : 'iradio_minimal-blue'
        });

        //下拉框
        $('.select2').select2();

        //建设单位查询组件
        $("#vCompanyNameInput").unitselCommpent({
            elmIdSel : "vCompanyNameInput",//需要展示的位置下拉框
            unitType : "10",//单位类型 为空的时候全部查询
            callbackFunc: function (objvalue, objname) {//下拉框值改变时回调
            	_querySubjects(1);
            }
        });
    }

    /**
     * 获取规划类型列表
     * @private
     */
    function _queryPlanTypes() {
        var req = {};
        var resp = client.callAsJson("projectInfoService/queryPlanProjClassConstList", req);
        if (!resp.success) {
            toastr.error(resp.resultMsg);
            return;
        }

        //规划类型列表添加元素
        var vPlanTypesSelect = $("#vPlanTypesSelect");
        $.each(resp.result, function (i, item) {
            if(i == 0) {
                vPlanTypesSelect.append("<option vaPlanTypeId='" + item.paramValValue + "' selected='selected'>" + item.paramValName + "</option>");
            } else {
                vPlanTypesSelect.append("<option vaPlanTypeId='" + item.paramValValue + "'>" + item.paramValName + "</option>");
            }
        });
    }

    /**
     * 查询项目进度列表
     * @param index
     * @param needTurn
     * @private
     */
    function _querySubjects(index) {
        if(com.im.projectInfoManage.itemStaticManage.subjectColumns == undefined) {
            com.im.projectInfoManage.itemStaticManage.subjectColumns = [[
                {field:'vaSeq',title:"序号",width:$(this).width()*0.03,fitColumns:true,formatter: function(value,row,index){return index + 1;}},
                {field:'impleProjName',title:"实施项目名称",width:$(this).width()*0.15,align:'left',fitColumns:true},
                {field:'planProjName',title:"规划工程名称",width:$(this).width()*0.15,align:'left',fitColumns:true},
                {field:'developOrgName',title:"建设单位",width:$(this).width()*0.15,align:'left',fitColumns:true},
                {field:'startDt',title:"开工年",width:$(this).width()*0.08,fitColumns:true},
                {field:'endDt',title:"完工年",width:$(this).width()*0.08,fitColumns:true},
                {field:'leadingPersonName',title:"项目负责人",width:$(this).width()*0.08,fitColumns:true},
                {field:'linkmanName',title:"项目联系人",width:$(this).width()*0.08,fitColumns:true},
                {field:'linkmanTel',title:"联系人电话",width:$(this).width()*0.08,fitColumns:true},
                {field:'projStageClassName',title:"当前阶段",width:$(this).width()*0.08,align:'left',fitColumns:true},
                {field:'curSituation',title:"最新进度",width:$(this).width()*0.12,align:'left',fitColumns:true},
                {field:'enterDt',title:"填报时间",width:$(this).width()*0.08,fitColumns:true},
                {field:'suggest',title:"问题和建议",width:$(this).width()*0.08,fitColumns:true,align:'left', formatter: function(value,row,index){
                if(value.length > 10){
                    return value.substr(0,10);
                }else{
                    return value;
                }
            }},
                {field:'statusName',title:"状态",width:$(this).width()*0.08,fitColumns:true}
            ]];
        }

        var req = {};
        req.pageInfo = {};
        req.pageInfo.pageNumber = index;
        req.pageInfo.pageSize = 20;
        req.operation = 'statis';
        //req.planProjClass = $("#vPlanTypesSelect option:selected").attr("value");
        req.planProjName = $("#vProjectNameInput").val();
        req.impleProjName = $("#vSubjectNameInput").val();
        var developOrg = $("#vCompanyNameInput option:selected").attr("value");
        if(developOrg != ''){
        	req.developOrg = developOrg;
        }else{
        	delete req.developOrg;
        }

        var resp = client.callAsJson("projectInfoService/queryProjProceSubmitList", req);
        if (!resp.success) {
            toastr.error(resp.resultMsg);
            return;
        } else {

        }

        plans = resp.result.rows;
        $("#vSubjectTable").empty();
        $("#vSubjectTable").datagrid({
    	    nowrap:false,
    	    fitColumns:true,
        	pagination:true,
        	columns:com.im.projectInfoManage.itemStaticManage.subjectColumns,
        	data:resp.result
        });
        var p = $('#vSubjectTable').datagrid('getPager');
        $(p).pagination({
            pageSize: 20,
            pageList: [10,20,30],
            beforePageText: '第',
            afterPageText: '页    共 {pages}  页',
            displayMsg: '当前显示 {from} - {to} 条记录 共 {total}  条记录',
            onSelectPage:function(pageNumber, pageSize){
                _querySubjects(pageNumber, false);
            }
        });
    }

    /**
     * 条件重置
     * @private
     */
    function _resetCond() {
        $("#vCompanyNameInput").val("").trigger("change");
        $("#vPlanTypesSelect").find("option:selected").removeAttr("selected");
        $("#vPlanTypesSelect").find("option").first().attr("selected", "selected");
        $("#vPlanTypesSelect").trigger("change");
        $("#vProjectNameInput").val("");
        $("#vSubjectNameInput").val("");
        _querySubjects(1);//重复调用
    }

    return {
        init : _init
    }
}();